package cn.fancy.sdk.utils;

import static android.content.Context.SENSOR_SERVICE;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import cn.fancy.sdk.common.utils.SdkAsyncTask;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.utils.HttpReq;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhaojh on date 2020/6/2
 */
public class JudgeSimulatorUtil {
    
    //true为真机
    public static boolean judgeTrueDevice(Context context) {
        boolean trueDevice = isHarmonyOS() || !isAndroidSimulator(context);
        Log.d("JudgeSimulatorUtil", "judgeTrueDevice" + trueDevice);
        return trueDevice;
    }
    
    private static boolean isAndroidSimulator(Context context) {
        if (!hasLightSensor(context)) {
            return true;
        }
        if (!canCallPhone(context)) {
            return true;
        }
        return Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.toLowerCase().contains("vbox") ||
            Build.FINGERPRINT.toLowerCase().contains("test-keys") || Build.MODEL.contains("google_sdk") ||
            Build.MODEL.contains("Emulator") || Build.MODEL.contains("virtual") ||
            Build.SERIAL.equalsIgnoreCase("android") || "google_sdk".equals(Build.PRODUCT) ||
            (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"));
    }
    
    // 是否可以处理跳转到拨号的 Intent
    private static boolean canCallPhone(Context context) {
        String url = "tel:" + "123456";
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_DIAL);
        return intent.resolveActivity(context.getPackageManager()) != null;
    }
    
    private static boolean hasLightSensor(Context context) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        return sensor != null;
    }
    
    //方法判断是否是模拟器，是则请求接口是否允许注册登录
    public static void sendSimulator(final Context context, String userName, int type, final SendSimulatorCallback sendSimulatorCallback) {
        if (judgeTrueDevice(context)) {
            sendSimulatorCallback.state(true);
            return;
        }
        final Map<String, String> stringMap = GuildBaseInfo.getInstance().sendIsSimulator(context, userName, type);
        SdkAsyncTask<String> sdkAsyncTask = new SdkAsyncTask<String>() {
            @Override
            public Activity getOwnerActivity() {
                return (Activity) context;
            }
            
            @Override
            protected String doInBackground() {
                return HttpReq.doGuildRequest(stringMap);
            }
            
            @Override
            protected void onPostExecute(String obj) {
                if (TextUtils.isEmpty(obj)) {
                    Toast.makeText(context, TipMessSingle.ten_networkerr, Toast.LENGTH_LONG).show();
                    return;
                }
                Log.e("sendSimulatorrst", "sendSimulatorrst:" + obj);
                try {
                    JSONObject json = new JSONObject(obj);
                    String state = json.getString("state");
                    JSONObject jsonState = new JSONObject(state);
                    int code = jsonState.getInt("code");
                    if (code != 1) {
                        Toast.makeText(context, "抱歉，该用户不处于白名单，请联系客服", Toast.LENGTH_LONG).show();
                        //阻断进游戏
                        sendSimulatorCallback.state(false);
                        return;
                    }
                    //此处进游戏
                    sendSimulatorCallback.state(true);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            
            @Override
            protected void onCancelled() {
            
            }
        };
        sdkAsyncTask.execute();
    }
    
    public interface SendSimulatorCallback {
        
        void state(boolean canEnterGame);
    }
    
    public static String simulatorParams(Context context) {
        return judgeTrueDevice(context) ? "0" : "1";
    }
    
    private static boolean isHarmonyOS() {
        try {
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            return "Harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable x) {
            x.printStackTrace();
            return false;
        }
    }
}
